package OjPracticeWithMyselfNote.LinkedList;

public class ReverseList {
    public class ListNode{
        int val;
        ListNode next = null;
        public ListNode(int val) {
            this.val = val;
        }
    }

    public class Solution {
    public ListNode ReverseList (ListNode head) {//按照先依次反转cur 节点和 前节点的顺序来迭代
        ListNode cur = head;
        ListNode prev = null;
        while(cur!=null) {
            ListNode curNext = cur.next;//保存curNext,因为要在迭代的同时把链表反转，若不对cur的next进行备份，则无法对链表进行遍历操作
            cur.next = prev;//先将cur节点进行反转再对 前节点和cur节点进行移动
            prev = cur;
            cur = curNext;
        }
        return prev;//最终cur指向原链表的最后面，所以前节点才是原链表的最后节点，新反转链表的头节点
    }
    }
}
